package view;

import com.example.googlemarketdemo.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;
/**
 * RatioLayout的布局，做屏幕适配
 * @author Anson-pc
 *
 */
public class RatioLayout extends FrameLayout {
	//图片的宽高比等于图片的宽高比
	private float mPicRatio = 0.0f;
	

	public void setmPicRatio(float mPicRatio) {
		this.mPicRatio = mPicRatio;
	}

	public static final int RELATIVE_WIDTH = 0;
	public static final int RELATIVE_HEIGHT = 1;
	
	private int relative=RELATIVE_WIDTH ;
	
	public void setRelative(int relative) {
		this.relative = relative;
	}

	public RatioLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		//获取属性
		TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.RatioLayout);
		
		mPicRatio = typedArray.getFloat(R.styleable.RatioLayout_picRatio, 0.0f);
		relative = typedArray.getInt(R.styleable.RatioLayout_relative, RELATIVE_WIDTH);
		
		typedArray.recycle();
	}

	public RatioLayout(Context context) {
		this(context,null);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		//获取控件宽高定义的模式
		int widthmode = MeasureSpec.getMode(widthMeasureSpec);
		
		//获取控件高度定义的模式
		int heightmode = MeasureSpec.getMode(heightMeasureSpec);
		if (widthmode == MeasureSpec.EXACTLY &&relative ==  RELATIVE_WIDTH) {
			//获取自身的宽度
			int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
			int parentHeight = (int) (parentWidth/mPicRatio +.5f);
			
			//孩子的高度和宽度,就是ImageView的宽高
			int childWidth = parentWidth - getPaddingLeft() - getPaddingRight();	
			int childHeight = parentHeight - getPaddingBottom() - getPaddingTop();
			
			//测量保存的结果
			setMeasuredDimension(parentWidth, parentHeight);
			
			//测绘孩子,告知孩子测绘方式，请求孩子测绘自身
			int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
			int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
			measureChildren(childWidthMeasureSpec, childheightMeasureSpec);
			
		}else if(heightmode == MeasureSpec.EXACTLY && relative == RELATIVE_HEIGHT){
			//获取自身的高度
			int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
			int parentWidth = (int) (mPicRatio*parentHeight+.5f);
			
			//孩子的高度和宽度,就是ImageView的宽高
			int childWidth = parentWidth - getPaddingLeft() - getPaddingRight();	
			int childHeight = parentHeight - getPaddingBottom() - getPaddingTop();
			
			//测量保存的结果
			setMeasuredDimension(parentWidth, parentHeight);
			
			//测绘孩子,告知孩子测绘方式，请求孩子测绘自身
			int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
			int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
			measureChildren(childWidthMeasureSpec, heightMeasureSpec);
			
		}else{
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
		
		
		
		
	}

}
